//
//  ViewController.swift
//  singer
//
//  Created by safeu on 2017/10/17.
//  Copyright © 2017年 xuyuanyuan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var index:Int!=0
    
    lazy var allArr:NSMutableArray={()->NSMutableArray in
        let tempallArr = NSMutableArray()
        let strPath=Bundle.main.path(forResource: "picList", ofType:"plist")
        let arr: NSArray=NSArray(contentsOfFile:strPath!)!
        for dict in arr{
            
            let model:singerModel=singerModel.singerModelWithDict(dict as!NSDictionary)
            tempallArr.add(model)
        }
        return tempallArr
}()
    
    lazy var titleLabel:UILabel={()->UILabel in
        let titleLabel = UILabel (frame:CGRect(x:0, y:20,width: 415,height: 100))
        titleLabel.backgroundColor=UIColor.white
        self.view.addSubview(titleLabel)
        return titleLabel
    }()
    
    lazy var imgview:UIImageView={()->UIImageView in
       let imgView = UIImageView(frame: CGRect(x: 0,y:100,width:180,height:320))
        imgView.center.x = self.view.bounds.width / 2
        imgView.backgroundColor=UIColor.black
        self.view.addSubview(imgView)
        return imgView
        
    }()
    
    lazy var btnBack:UIButton={()->UIButton  in
        let btnBack = UIButton(frame:CGRect(x:50,y:450,width:self.view.bounds.width / 2,height: 50))
        btnBack.setTitle("上一张", for:UIControlState.normal)
        btnBack.addTarget(self, action: #selector(setBack), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btnBack)
        return btnBack
    }()
    
    lazy var btnNext:UIButton={()->UIButton  in
        let btnNext = UIButton(frame:CGRect(x:self.view.bounds.width / 2,y:450,width:self.view.bounds.width / 2 - 50,height: 50))
        btnNext.setTitle("下一张", for:UIControlState.normal)
        btnNext.addTarget(self, action: #selector(setNext), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btnNext)
        return btnNext
    }()
    
    func setBack(){
        if self.index>0&&self.index<=self.allArr.count-1{
            self.index=self.index-1
            let model:singerModel=self.allArr[self.index]as!singerModel
            let titleStr=NSString(string: "\(model.name!)\(self.index+1)/\(self.allArr.count)")
            self.titleLabel.text=titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgview.image=UIImage(named: model.pic as String)
            self.btnNext.isEnabled=true
            self.btnNext.backgroundColor=UIColor.darkGray
            if self.index==0{
                self.btnBack.isEnabled=false
                self.btnBack.backgroundColor=UIColor.blue
            }
        }
    }
    
    func setNext(){
        if self.index>=0&&self.index<self.allArr.count-1{
            self.index=self.index+1
            let model:singerModel=self.allArr[self.index]as!singerModel
            let titleStr=NSString(string: "\(model.name!)\("    ")\(self.index+1)/\(self.allArr.count)")
            self.titleLabel.text=titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgview.image=UIImage(named: model.pic as String)
            self.btnBack.isEnabled=true
            self.btnBack.backgroundColor=UIColor.darkGray
            if self.index==self.allArr.count-1{
                self.btnNext.isEnabled=false
                self.btnNext.backgroundColor=UIColor.blue
            }
            
        }
    }
    
    func showCurrentPage(iPage: Int){
        let model:singerModel=self.allArr[iPage]as!singerModel
        let titleStr = NSString(string:"\(model.name!)\(self.index+1)/\(self.allArr.count)")
        self.titleLabel.text=titleStr as String;
        self.titleLabel.textAlignment = .center
        self.imgview.image=UIImage(named:model.pic as String)
        self.btnBack.backgroundColor=UIColor.darkGray
        self.btnNext.backgroundColor=UIColor.darkGray
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.showCurrentPage(iPage:self.index)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

